package cz.proks.simplecalendarmanager.server.servlet;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;

import cz.proks.simplecalendarmanager.server.Configuration;
import cz.proks.simplecalendarmanager.shared.ClientConstants;
import cz.proks.simplecalendarmanager.shared.model.AccessToken;

public abstract class GAuthentificatedRemoteServiceServlet extends SCMRemoteServiceServlet {
	
	protected final void authentificate(GoogleService service, ClientConstants.AppScope scope) throws OAuthException {
		AccessToken accessToken = getAccessTokenFromSession(scope.getIdentifier()); 
		
		GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
		oauthParameters.setOAuthConsumerKey(Configuration.getConsumerToken());
		oauthParameters.setOAuthConsumerSecret(Configuration.getConsumerTokenSecret());
		
		oauthParameters.setOAuthToken(accessToken.getOAuthToken());
		oauthParameters.setOAuthTokenSecret(accessToken.getOAuthTokenSecret());
		
		service.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
	}
	
	protected abstract <T extends GoogleService> T getAuthentificatedService() throws OAuthException;	
		
}
